<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.code }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <FormImport @register="registerImportModal" @success="handleSuccess" />
    <AddQtyModal @register="registerUseQtyModal" @success="handleSuccess" />
    <PrintModal @register="registerPrintModal" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsBasMoldBasMoldSearchList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, watch } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useGlobSetting } from '/@/hooks/setting';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { basMoldDelete, basMoldListData } from '/@/api/bas/mold/basMold';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import FormImport from './formImport.vue';
  import AddQtyModal from './addQtyModal.vue';
  import { downloadByUrl } from '/@/utils/file/download';
  import { basMoldClsTreeData } from '/@/api/bas/mold/basMoldCls';
  import PrintModal from '/@/components/print/printModal.vue';

  const props = defineProps({
    treeCode: String,
    treeName: String,
  });

  const { t } = useI18n('bas.mold.basMold');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('工装档案管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    autoAdvancedLine: 10,
    schemas: [
      {
        label: t('工装代码'),
        field: 'caddcode',
        component: 'Input',
      },
      {
        label: t('工装编码'),
        field: 'code',
        component: 'Input',
      },
      {
        label: t('工装名称'),
        field: 'name',
        component: 'Input',
      },
      {
        label: t('工装类型'),
        field: 'clsCode',
        fieldLabel: 'basMold.basMoldCls.treeName',
        component: 'TreeSelect',
        componentProps: {
          api: basMoldClsTreeData,
          allowClear: true,
          onChange: handleSuccess,
        },
      },

      {
        label: t('工装状态'),
        field: 'moldStatusIn',
        component: 'Select',
        componentProps: {
          dictType: 'bas_mold_status',
          mode: 'multiple',
          onChange: handleSuccess,
          allowClear: true,
        },
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('工装代码'),
      dataIndex: 'caddcode',
      key: 'a.caddcode',
      sorter: true,
      width: 130,
      align: 'left',
      //fixed: 'left',
    },
    {
      title: t('工装编码'),
      dataIndex: 'code',
      key: 'a.code',
      sorter: true,
      width: 230,
      align: 'left',
      fixed: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('工装状态'),
      dataIndex: 'moldStatus',
      key: 'a.mold_status',
      sorter: true,
      dictType: 'bas_mold_status',
      width: 100,
      align: 'center',
      //fixed: 'left',
    },
    {
      title: t('工装尾号'),
      dataIndex: 'lastNum',
      key: 'a.lastNum',
      sorter: true,
      width: 90,
      align: 'left',
      fixed: 'left',
    },
    {
      title: t('工装名称'),
      dataIndex: 'name',
      key: 'a.name',
      sorter: true,
      width: 230,
      align: 'left',
      fixed: 'left',
    },
    {
      title: t('工装类型'),
      dataIndex: 'basMoldCls.name',
      key: 'cls.code',
      sorter: true,
      width: 100,
      align: 'center',
      //fixed: 'left',
    },
  ];

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm }] = useTable({
    api: basMoldListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  watch(
    () => props.treeCode,
    async () => {
      await getForm().setFieldsValue({
        clsCode: props.treeCode,
        'basMold.basMoldCls.treeName': props.treeName,
      });
      reload();
    },
  );

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  // 增加使用次数

  function handleSuccess() {
    reload();
  }

</script>
